# /usr/bin/python
# coding: utf-8
import configparser
import os
from db import dbUtil
import util
import sys


def main(argv):
    # 读取配置
    if os.access("./init.ini", os.F_OK):
        config = configparser.ConfigParser()
        config.read("init.ini")
        dbConfig = {
            'host': config.get("DB", "host"),
            'port': int(config.get("DB", "port")),
            'user': config.get("DB", "user"),
            'passwd': config.get("DB", "passwd"),
            'db': config.get("DB", "db"),
            'charset': "utf8"
        }
        table = config.get("DB", "table")
        print("配置文件读取成功！")
        print("-"*20)
        # 初始化数据库
        db = dbUtil(dbConfig, table)
        # 获取该表字段列表
        field = db.getDbField()
        name = config.get("Parameter", "name")
        idCard = config.get("Parameter", "idCard")
        targetName = config.get("Parameter", "targetName")
        targetFirstLetter = config.get("Parameter", "targetFirstLetter")
        key = config.get("Parameter", "key")
        lastUpdate = config.get("Parameter", "lastUpdate")
        targetBirthday = config.get("Parameter", "targetBirthday")
        mode = config.get("Options", 'mode')
        if name:
            if 'full_letter' in mode:
                if util.checkField(name, field):
                    print(name, "字段检测成功！")
                    db.fieldToPinYin(name, key, targetName, lastUpdate)
                else:
                    print(name, '未检测到此字段！')
            if 'first_letter' in mode:
                if util.checkField(name, field):
                    print(name, "字段检测成功！")
                    db.fieldToFirstLetter(
                        name, key, targetFirstLetter, lastUpdate)
                else:
                    print(name, "未检测到此字段!")
        if idCard and 'birthday' in mode:
            if util.checkField(idCard, field):
                print(idCard, "字段检测成功！")
                db.getRecordCount()
                db.fieldToBirthday(idCard, key, targetBirthday, lastUpdate)
            else:
                print("数据库中不包含此字段:", idCard)
        print("执行完成，请查看生成的文件")
    else:
        print("配置文件不存在")


if __name__ == "__main__":
    main(sys.argv[1:])
